<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>10.面向对象、正则表达式 | winnnntttter</title>
    <meta name="description" content="">
    <meta name="generator" content="VuePress 1.8.0">
    <link rel="icon" href="/favicon.ico">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css">
    <script>
      var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?7ae3e8df51439cebe7b2a43bccd0b253";
        var s = document.getElementsByTagName("script")[0]; 
        s.parentNode.insertBefore(hm, s);
      })();
      </script>
    <meta name="description" content="winnnntttter的个人站">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    <link rel="preload" href="/assets/css/0.styles.a3a399df.css" as="style"><link rel="preload" href="/assets/js/app.419c42a0.js" as="script"><link rel="preload" href="/assets/js/3.2e7f4496.js" as="script"><link rel="preload" href="/assets/js/31.f0b09253.js" as="script"><link rel="prefetch" href="/assets/js/10.bd166216.js"><link rel="prefetch" href="/assets/js/100.ef6c4e60.js"><link rel="prefetch" href="/assets/js/101.d01225fc.js"><link rel="prefetch" href="/assets/js/102.b85e1bb3.js"><link rel="prefetch" href="/assets/js/103.efa64f76.js"><link rel="prefetch" href="/assets/js/104.e62be1cd.js"><link rel="prefetch" href="/assets/js/105.4db56e9c.js"><link rel="prefetch" href="/assets/js/106.f6ccef65.js"><link rel="prefetch" href="/assets/js/11.705ff9c2.js"><link rel="prefetch" href="/assets/js/12.7f08fe83.js"><link rel="prefetch" href="/assets/js/13.c9276fff.js"><link rel="prefetch" href="/assets/js/14.03705b05.js"><link rel="prefetch" href="/assets/js/15.a39a18a1.js"><link rel="prefetch" href="/assets/js/16.ecd9a98c.js"><link rel="prefetch" href="/assets/js/17.cfa099fe.js"><link rel="prefetch" href="/assets/js/18.be92763c.js"><link rel="prefetch" href="/assets/js/19.8a4801fa.js"><link rel="prefetch" href="/assets/js/20.bfc1f073.js"><link rel="prefetch" href="/assets/js/21.08ea81e0.js"><link rel="prefetch" href="/assets/js/22.635463a2.js"><link rel="prefetch" href="/assets/js/23.fcf1e19e.js"><link rel="prefetch" href="/assets/js/24.e4fe2627.js"><link rel="prefetch" href="/assets/js/25.c64ebd97.js"><link rel="prefetch" href="/assets/js/26.b83aa917.js"><link rel="prefetch" href="/assets/js/27.9c0001f7.js"><link rel="prefetch" href="/assets/js/28.467acfc5.js"><link rel="prefetch" href="/assets/js/29.b88eaf80.js"><link rel="prefetch" href="/assets/js/30.79fca896.js"><link rel="prefetch" href="/assets/js/32.680d05e2.js"><link rel="prefetch" href="/assets/js/33.06db5f96.js"><link rel="prefetch" href="/assets/js/34.9b7ffb83.js"><link rel="prefetch" href="/assets/js/35.b302d83e.js"><link rel="prefetch" href="/assets/js/36.8ce84d21.js"><link rel="prefetch" href="/assets/js/37.8f1286d0.js"><link rel="prefetch" href="/assets/js/38.0eba62cc.js"><link rel="prefetch" href="/assets/js/39.9eafaf04.js"><link rel="prefetch" href="/assets/js/4.10fce449.js"><link rel="prefetch" href="/assets/js/40.94e167a7.js"><link rel="prefetch" href="/assets/js/41.9b634e18.js"><link rel="prefetch" href="/assets/js/42.a3ad7d5f.js"><link rel="prefetch" href="/assets/js/43.9ad38a47.js"><link rel="prefetch" href="/assets/js/44.1d5a8a2c.js"><link rel="prefetch" href="/assets/js/45.45af274c.js"><link rel="prefetch" href="/assets/js/46.45a0da7e.js"><link rel="prefetch" href="/assets/js/47.e31b4c4f.js"><link rel="prefetch" href="/assets/js/48.2b1a4ffb.js"><link rel="prefetch" href="/assets/js/49.e4a82034.js"><link rel="prefetch" href="/assets/js/5.edc6b3fb.js"><link rel="prefetch" href="/assets/js/50.8b0a75d7.js"><link rel="prefetch" href="/assets/js/51.fb58d1f2.js"><link rel="prefetch" href="/assets/js/52.aba39728.js"><link rel="prefetch" href="/assets/js/53.d683a969.js"><link rel="prefetch" href="/assets/js/54.c91b9f4c.js"><link rel="prefetch" href="/assets/js/55.76d17213.js"><link rel="prefetch" href="/assets/js/56.5935f2ad.js"><link rel="prefetch" href="/assets/js/57.65d32203.js"><link rel="prefetch" href="/assets/js/58.0b637d75.js"><link rel="prefetch" href="/assets/js/59.4f569adb.js"><link rel="prefetch" href="/assets/js/6.fe59ecb4.js"><link rel="prefetch" href="/assets/js/60.3f56e6a2.js"><link rel="prefetch" href="/assets/js/61.a6beb596.js"><link rel="prefetch" href="/assets/js/62.db0af84e.js"><link rel="prefetch" href="/assets/js/63.1d585303.js"><link rel="prefetch" href="/assets/js/64.3861310e.js"><link rel="prefetch" href="/assets/js/65.9b1ef5df.js"><link rel="prefetch" href="/assets/js/66.dd5713f3.js"><link rel="prefetch" href="/assets/js/67.fa823bb5.js"><link rel="prefetch" href="/assets/js/68.ae4e8b4f.js"><link rel="prefetch" href="/assets/js/69.b36498fb.js"><link rel="prefetch" href="/assets/js/7.04e3d394.js"><link rel="prefetch" href="/assets/js/70.8f75877a.js"><link rel="prefetch" href="/assets/js/71.8b54fe2e.js"><link rel="prefetch" href="/assets/js/72.7892e629.js"><link rel="prefetch" href="/assets/js/73.41e6a3ad.js"><link rel="prefetch" href="/assets/js/74.a8d720c9.js"><link rel="prefetch" href="/assets/js/75.677c8235.js"><link rel="prefetch" href="/assets/js/76.9c7df0e7.js"><link rel="prefetch" href="/assets/js/77.1e4ef451.js"><link rel="prefetch" href="/assets/js/78.503ea59d.js"><link rel="prefetch" href="/assets/js/79.e473582d.js"><link rel="prefetch" href="/assets/js/8.35d757ca.js"><link rel="prefetch" href="/assets/js/80.71d04c61.js"><link rel="prefetch" href="/assets/js/81.36d69e64.js"><link rel="prefetch" href="/assets/js/82.9491f2b1.js"><link rel="prefetch" href="/assets/js/83.d9808b8b.js"><link rel="prefetch" href="/assets/js/84.e0231735.js"><link rel="prefetch" href="/assets/js/85.ddc29777.js"><link rel="prefetch" href="/assets/js/86.05cd7dc6.js"><link rel="prefetch" href="/assets/js/87.1e74cca5.js"><link rel="prefetch" href="/assets/js/88.957d6dbb.js"><link rel="prefetch" href="/assets/js/89.b216c556.js"><link rel="prefetch" href="/assets/js/9.07b2db50.js"><link rel="prefetch" href="/assets/js/90.0c71a3f2.js"><link rel="prefetch" href="/assets/js/91.038eb70a.js"><link rel="prefetch" href="/assets/js/92.f05b7ced.js"><link rel="prefetch" href="/assets/js/93.e1cd3406.js"><link rel="prefetch" href="/assets/js/94.fe506104.js"><link rel="prefetch" href="/assets/js/95.440323e0.js"><link rel="prefetch" href="/assets/js/96.9ee51a0b.js"><link rel="prefetch" href="/assets/js/97.826f8538.js"><link rel="prefetch" href="/assets/js/98.15cf8919.js"><link rel="prefetch" href="/assets/js/99.489906fc.js"><link rel="prefetch" href="/assets/js/vendors~docsearch.f879b1e1.js">
    <link rel="stylesheet" href="/assets/css/0.styles.a3a399df.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="ant-row"><div class="sidebar-button"><i aria-label="icon: bars" class="anticon anticon-bars"><svg viewBox="0 0 1024 1024" focusable="false" data-icon="bars" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M912 192H328c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h584c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm0 284H328c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h584c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm0 284H328c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h584c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zM104 228a56 56 0 1 0 112 0 56 56 0 1 0-112 0zm0 284a56 56 0 1 0 112 0 56 56 0 1 0-112 0zm0 284a56 56 0 1 0 112 0 56 56 0 1 0-112 0z"></path></svg></i> <span></span></div> <div class="ant-col ant-col-xs-24 ant-col-sm-24 ant-col-md-6 ant-col-lg-5 ant-col-xl-5 ant-col-xxl-4"><a href="/" class="router-link-active home-link"><img src="/logo.jpg" alt="winnnntttter" class="logo"> <span class="site-name">winnnntttter</span></a> <div class="search-box mobile-search"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div></div> <div class="ant-col ant-col-xs-0 ant-col-sm-0 ant-col-md-18 ant-col-lg-19 ant-col-xl-19 ant-col-xxl-20"><form id="search-form" role="search" class="algolia-search-wrapper search-box"><input id="algolia-search-input" class="search-query"></form> <nav class="nav-links can-hide"><ul role="menu" id="nav" class="ant-menu ant-menu-horizontal ant-menu-root ant-menu-light"><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item"><a href="/" class="router-link-active">
          首页
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-submenu-selected"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>
          🍃前端
        </span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item"><a href="/Flutter/">
          🌿Flutter
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>
          🌱其他
        </span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item"><a href="https://jsbin.com/" target="_blank">
          🏃‍♂️运行
          <i aria-label="icon: link" class="anticon anticon-link"><svg viewBox="64 64 896 896" focusable="false" data-icon="link" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M574 665.4a8.03 8.03 0 0 0-11.3 0L446.5 781.6c-53.8 53.8-144.6 59.5-204 0-59.5-59.5-53.8-150.2 0-204l116.2-116.2c3.1-3.1 3.1-8.2 0-11.3l-39.8-39.8a8.03 8.03 0 0 0-11.3 0L191.4 526.5c-84.6 84.6-84.6 221.5 0 306s221.5 84.6 306 0l116.2-116.2c3.1-3.1 3.1-8.2 0-11.3L574 665.4zm258.6-474c-84.6-84.6-221.5-84.6-306 0L410.3 307.6a8.03 8.03 0 0 0 0 11.3l39.7 39.7c3.1 3.1 8.2 3.1 11.3 0l116.2-116.2c53.8-53.8 144.6-59.5 204 0 59.5 59.5 53.8 150.2 0 204L665.3 562.6a8.03 8.03 0 0 0 0 11.3l39.8 39.8c3.1 3.1 8.2 3.1 11.3 0l116.2-116.2c84.5-84.6 84.5-221.5 0-306.1zM610.1 372.3a8.03 8.03 0 0 0-11.3 0L372.3 598.7a8.03 8.03 0 0 0 0 11.3l39.6 39.6c3.1 3.1 8.2 3.1 11.3 0l226.4-226.4c3.1-3.1 3.1-8.2 0-11.3l-39.5-39.6z"></path></svg></i></a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>
          🌈联系
        </span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="visibility:hidden;position:absolute;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li></ul> <a href="https://github.com/winnnntttter/winnnntttter.github.io" target="_blank" rel="noopener noreferrer" class="repo-link"><i aria-label="icon: github" class="anticon anticon-github"><svg viewBox="64 64 896 896" focusable="false" data-icon="github" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M511.6 76.3C264.3 76.2 64 276.4 64 523.5 64 718.9 189.3 885 363.8 946c23.5 5.9 19.9-10.8 19.9-22.2v-77.5c-135.7 15.9-141.2-73.9-150.3-88.9C215 726 171.5 718 184.5 703c30.9-15.9 62.4 4 98.9 57.9 26.4 39.1 77.9 32.5 104 26 5.7-23.5 17.9-44.5 34.7-60.8-140.6-25.2-199.2-111-199.2-213 0-49.5 16.3-95 48.3-131.7-20.4-60.5 1.9-112.3 4.9-120 58.1-5.2 118.5 41.6 123.2 45.3 33-8.9 70.7-13.6 112.9-13.6 42.4 0 80.2 4.9 113.5 13.9 11.3-8.6 67.3-48.8 121.3-43.9 2.9 7.7 24.7 58.3 5.5 118 32.4 36.8 48.9 82.7 48.9 132.3 0 102.2-59 188.1-200 212.9a127.5 127.5 0 0 1 38.1 91v112.5c.8 9 0 17.9 15 17.9 177.1-59.7 304.6-227 304.6-424.1 0-247.2-200.4-447.3-447.5-447.3z"></path></svg></i></a></nav></div></div> <!----></header> <aside class="sidebar"><!----> <ul class="sidebar-links"><li><a href="/front-end/js/" aria-current="page" title="js" class="sidebar-link">js</a></li><li><a href="/front-end/js/1.全选、函数参数、下拉框、五星评价.html" title="1.全选、函数参数、下拉框、五星评价" class="sidebar-link">1.全选、函数参数、下拉框、五星评价</a></li><li><a href="/front-end/js/10.面向对象、正则表达式.html" title="10.面向对象、正则表达式" class="active sidebar-link">10.面向对象、正则表达式</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/front-end/js/10.面向对象、正则表达式.html#面向对象" title="面向对象" class="sidebar-link">面向对象</a></li><li class="sidebar-sub-header"><a href="/front-end/js/10.面向对象、正则表达式.html#正则表达式" title="正则表达式" class="sidebar-link">正则表达式</a></li></ul></li><li><a href="/front-end/js/11.web基本概念、ajax.html" title="11.web基本概念、ajax" class="sidebar-link">11.web基本概念、ajax</a></li><li><a href="/front-end/js/12.Array.reduce().html" title="12.Array.reduce()" class="sidebar-link">12.Array.reduce()</a></li><li><a href="/front-end/js/13.验证浏览器信息.html" title="13.验证浏览器信息" class="sidebar-link">13.验证浏览器信息</a></li><li><a href="/front-end/js/2.计时器、无缝滚动、选项卡、typeof 和instanceof.html" title="2.计时器、无缝滚动、选项卡、typeof 和instanceof" class="sidebar-link">2.计时器、无缝滚动、选项卡、typeof 和instanceof</a></li><li><a href="/front-end/js/3.各种width、拨号排列、马赛克tab、拖动、碰撞、模拟滚动条、放大镜.html" title="3.各种width、拨号排列、马赛克tab、拖动、碰撞、模拟滚动条、放大镜" class="sidebar-link">3.各种width、拨号排列、马赛克tab、拖动、碰撞、模拟滚动条、放大镜</a></li><li><a href="/front-end/js/4.拖动放大图片、图片中放大镜、页面返回和前进、滚动、错误提示.html" title="4.拖动放大图片、图片中放大镜、页面返回和前进、滚动、错误提示" class="sidebar-link">4.拖动放大图片、图片中放大镜、页面返回和前进、滚动、错误提示</a></li><li><a href="/front-end/js/5.插入表格、事件监听、事件源、贴标签、清除空白节点、仿mac、随机颜色、自定义右键菜单.html" title="5.插入表格、事件监听、事件源、贴标签、清除空白节点、仿mac、随机颜色、自定义右键菜单" class="sidebar-link">5.插入表格、事件监听、事件源、贴标签、清除空白节点、仿mac、随机颜色、自定义右键菜单</a></li><li><a href="/front-end/js/6.倒计时、日期、图片时钟、双色球、区分中英文计算字数、判断上下屏、星座、完整日历、作用域.html" title="6.倒计时、日期、图片时钟、双色球、区分中英文计算字数、判断上下屏、星座、完整日历、作用域" class="sidebar-link">6.倒计时、日期、图片时钟、双色球、区分中英文计算字数、判断上下屏、星座、完整日历、作用域</a></li><li><a href="/front-end/js/7.json数据、获取css样式、自运行函数、回调函数、闭包.html" title="7.json数据、获取css样式、自运行函数、回调函数、闭包" class="sidebar-link">7.json数据、获取css样式、自运行函数、回调函数、闭包</a></li><li><a href="/front-end/js/8.二级菜单、运动函数、侧边悬停、轮播图.html" title="8.二级菜单、运动函数、侧边悬停、轮播图" class="sidebar-link">8.二级菜单、运动函数、侧边悬停、轮播图</a></li><li><a href="/front-end/js/9.数据存储、js编译、穷举法买鸡.html" title="9.数据存储、js编译、穷举法买鸡" class="sidebar-link">9.数据存储、js编译、穷举法买鸡</a></li><li><a href="/front-end/js/blob和fileReader.html" title="blob和fileReader" class="sidebar-link">blob和fileReader</a></li><li><a href="/front-end/js/bom与dom.html" title="bom与dom" class="sidebar-link">bom与dom</a></li><li><a href="/front-end/js/getParamName.html" title="getParamName" class="sidebar-link">getParamName</a></li><li><a href="/front-end/js/js内置对象.html" title="js内置对象" class="sidebar-link">js内置对象</a></li><li><a href="/front-end/js/xhr下载文件.html" title="xhr下载文件" class="sidebar-link">xhr下载文件</a></li><li><a href="/front-end/js/判断空对象.html" title="判断空对象" class="sidebar-link">判断空对象</a></li><li><a href="/front-end/js/原生加className，判断是否显示.html" title="原生加className，判断是否显示" class="sidebar-link">原生加className，判断是否显示</a></li><li><a href="/front-end/js/固定字数显示.html" title="固定字数显示" class="sidebar-link">固定字数显示</a></li><li><a href="/front-end/js/移动端适配.html" title="移动端适配" class="sidebar-link">移动端适配</a></li><li><a href="/front-end/js/获取服务器时间.html" title="获取服务器时间" class="sidebar-link">获取服务器时间</a></li></ul></aside> <main class="page"> <div class="theme-antdocs-content content__default"><h1 id="_10-面向对象、正则表达式"><a href="#_10-面向对象、正则表达式" class="header-anchor">#</a> 10.面向对象、正则表达式</h1> <h2 id="面向对象"><a href="#面向对象" class="header-anchor">#</a> 面向对象</h2> <p>对象 = 数据 + 对数据的操作 (称为封装）<br>
var emp = new Object();
JS 允许使用 new 关键字，后面调用一个函数，叫做 Object()。这种方式会向系统申请一堆内存空间用来存放一个员工的信息(工号、姓名和性别)<br>
var emp;
emp = new Object(); 凡是使用==new==关键字申请的内存，都是在“==堆==”中分配的
这句指令，会向系统申请空间，并所分配的内存空间的首地址，赋给左侧的变量 emp<br>
使用点运算符来引用对象中的变量:向“员工信息登记表”中填写各项信息<br>
emp.id = &quot;1001&quot;;
emp.name = &quot;张三&quot;;
emp.gender = &quot;男&quot;;\</p> <p>var emp01 = {
id: &quot;1001&quot;,
name:&quot;张三&quot;,
gender:&quot;男&quot;
};与上述等价</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> account <span class="token operator">=</span> <span class="token punctuation">{</span>
  id<span class="token operator">:</span> <span class="token string">&quot;00001&quot;</span><span class="token punctuation">,</span>
  name<span class="token operator">:</span> <span class="token string">&quot;张三&quot;</span><span class="token punctuation">,</span>
  balance<span class="token operator">:</span> <span class="token number">1000</span><span class="token punctuation">,</span>
  <span class="token comment">//存款</span>
  <span class="token function-variable function">deposit</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">money</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>money <span class="token operator">&lt;=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span>balance <span class="token operator">+=</span> money<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token comment">//取款</span>
  <span class="token function-variable function">withdrew</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">money</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>money <span class="token operator">&gt;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>balance<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;您的余额已不足！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span>balance <span class="token operator">-=</span> money<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token function-variable function">info</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;您的账户余额为：&quot;</span> <span class="token operator">+</span> <span class="token keyword">this</span><span class="token punctuation">.</span>balance<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
account<span class="token punctuation">.</span><span class="token function">deposit</span><span class="token punctuation">(</span><span class="token number">10000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
account<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
account<span class="token punctuation">.</span><span class="token function">withdrew</span><span class="token punctuation">(</span><span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
account<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
account<span class="token punctuation">.</span><span class="token function">withdrew</span><span class="token punctuation">(</span><span class="token number">12000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="正则表达式"><a href="#正则表达式" class="header-anchor">#</a> 正则表达式</h2> <p>正则表达式就是记录文本规则的代码<br>
作用：</p> <ol><li>数据验证： 可以验证输入字符串格式是否正确，比如邮箱地址的格式是否合法。</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 定义正则表达式，指定模式规则</span>
<span class="token keyword">var</span> pattern <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^[a-zA-Z][a-za-z0-9_]{4,15}$</span><span class="token regex-delimiter">/</span><span class="token regex-flags">i</span></span><span class="token punctuation">;</span>
<span class="token comment">//^起始字符 []从中间选一个</span>
<span class="token comment">//[]后跟选取范围{}重复4-15次</span>
<span class="token comment">//$结束字符</span>
<span class="token comment">//i 不区分大小写，可省略 A-Z</span>

<span class="token comment">// 调用正则对象的test()方法来验证参数username是否符合正则指定的规则</span>
<span class="token comment">// test()方法的返回值为boolean: true或false</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>pattern<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>username<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// 说明格式有效</span>
  document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">&quot;msg&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>innerHTML <span class="token operator">=</span> <span class="token string">&quot;有效输入&quot;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
  <span class="token comment">// 说明格式无效</span>
  document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">&quot;msg&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>innerHTML <span class="token operator">=</span> <span class="token string">&quot;输入格式不正确&quot;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><ol start="2"><li>替换文本：可以使用正则表达式来识别文档中的特定文本，替换或删除该文本。</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> pattern <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">枪|台独|法轮功</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span><span class="token comment">//全局匹配 g,否则只匹配第一个</span>
content <span class="token operator">=</span> content<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>pattern<span class="token punctuation">,</span><span class="token string">&quot;***&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
String<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token punctuation">)</span>方法的第二个参数，也可以是一个函数。该函数接收匹配到的文本作为参数，并返回替代文本：
content <span class="token operator">=</span> content<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>pattern<span class="token punctuation">,</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">matchText</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token string">&quot;aaa&quot;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><ol start="3"><li>提取字符串：可以使用正则表达式来查找文档内或输入域内特定的文本。</li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> log <span class="token operator">=</span> <span class="token string">'205.212.115.106 - - [01/Jul/1995:00:00:12 -0400] &quot;GET /shuttle/countdown/countdown.html HTTP/1.0&quot; 200 3985'</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> pattern <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\d+\.\d+\.\d+\.\d+</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span> <span class="token comment">// ip的模式  \d+:意思是数字出现1个或多个</span>
<span class="token keyword">var</span> arr <span class="token operator">=</span> log<span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span>pattern<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 如果匹配到了，返回一个匹配字符串的数组</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;客户的IP地址是：&quot;</span> <span class="token operator">+</span> arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>基本规则
i:不区分大小写。g：全局匹配。匹配所有的模式实例，而不仅仅是第一个(默认).m：匹配多行(可能是从 textarea 元素获得的)。
如果想要匹配一系列字符，可以将这些字符放入==字符集==[]。例如，[abc]意味着任何的字符 a、b 或 c：
如果想匹配除了以^指定的模式之外的任何内容：[^abc]
可以使用连字符“-”定义一个字符范围作为字符集。/ [0-9]/匹配 0 到 9 之间的单个数字，也可以使用不止一个范围，例如/[A-Za-z0-9]/匹配字母与数字。</p> <p>正则有 test 和 exec 方法，string 有 replace，match，search，split 方法</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> reg <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">test</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">;</span>
<span class="token keyword">var</span> str <span class="token operator">=</span> <span class="token string">&quot;testStringtest&quot;</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> result1 <span class="token operator">=</span> reg<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>result1<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//[&quot;test&quot;, index: 0, input: &quot;testStringtest&quot;]</span>
<span class="token keyword">var</span> result2 <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span>reg<span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>result2<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//[&quot;test&quot;, &quot;test&quot;]</span>
</code></pre></div><p>split 方法:用符合正则表达式条件的字符将原字符串分解为字符串数组</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> string3 <span class="token operator">=</span> <span class="token string">&quot;1,2, 3, 4, 5&quot;</span><span class="token punctuation">;</span>
parsestring3 <span class="token operator">=</span> string3<span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\s*,\s*</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//返回数组：[&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,&quot;5&quot;]</span>
<span class="token comment">// *匹配前面的子表达式任意次。例如，zo*能匹配“z”，也能匹配“zo”以及“zoo”。</span>
<span class="token comment">// \s 匹配任何不可见字符，包括空格、制表符、换页符等等。等价于[ \f\n\r\t\v]。</span>
<span class="token comment">// \S 匹配任意不是空白符的字符，与\s 相反</span>
<span class="token comment">// \D 匹配任意非数字的字符，与\d 相反</span>
<span class="token comment">// \w 绝大多数的正则表达式实现的单词字符集都包含了[A-Za-z0-9_]</span>
<span class="token comment">// \W 匹配任意不是字母和数字的字符，与\w 相反</span>

<span class="token comment">// - 匹配前面的子表达式零次或多次。</span>

<span class="token comment">// * 匹配前面的子表达式一次或多次。</span>
<span class="token comment">//   ? 匹配前面的子表达式零次或一次</span>
<span class="token comment">//   {n} 重复 n 次</span>
<span class="token comment">//   {n,} 重复 n 次或更多次</span>
<span class="token comment">//   {n,m} 重复 n 到 m 次</span>

<span class="token comment">// ()标记一个子表达式的开始和结束位置。子表达式可以获取供以后使用</span>
<span class="token comment">// [ ] 标记一个字符集表达式的开始和结束</span>
<span class="token comment">// | 指明两项之间的一个选择</span>
</code></pre></div><p>search(正则表达式):搜索字符串中是否含有满足正则表达式条件的字符串，若有，返回 true，否则，返回 false</p> <p>常用正则表达式</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// ^[a-zA-Z][a-za-z0-9_]{4,15}$ 字母开头，允许 5-16 字节，允许字母数字下划线</span>
<span class="token comment">// \d{3}-\d{8}|\d{4}-\d{7} 匹配国内电话号码</span>
<span class="token comment">// ^1\d{10}$手机号</span>
<span class="token comment">// ^\d{6}$邮编</span>
<span class="token comment">// [1-9][0-9]{4,} QQ 号从 10000 开始</span>
<span class="token comment">// \d+\.\d+\.\d+\.\d+ p 地址</span>
<span class="token comment">// [1-9]\d 10-99</span>
<span class="token comment">// \d 0-9</span>
<span class="token comment">// 1[0-1]\d 100-119</span>
<span class="token comment">// (1[0-1]|[1-9])?\d 0-119</span>
<span class="token comment">// /^120$|^((1[0-1]|[1-9])?\d)$/m 0-120</span>
<span class="token comment">// /^((19\d{2})|(200\d))-(0?[1-9]|1[0-2])-(0?[1-9]|[1-2]\d|3[0-1])$/ 生日的年份在 1900-2009 之间，生日格式为 1980-5-12 或 1988-05-04</span>

<span class="token comment">// 至少包含 2 个数字 5-18 位/^(?=._\d{2,})[A-Za-z\d]{5,18}$/ /^(?=[A-Za-z\d]_[\d][a-za-z\d]_[\d])[A-Za-z\d]{5,18}$/</span>
<span class="token comment">// 至少包含数字、字母、下划线三种里的两种/(?:\d.__)|(?:_._\d)|(?:[A-Za-z].__)|(?:_._[A-Za-z])|(?:[A-Za-z]._\d)|(?:\d.\*[A-Za-z])/</span>
</code></pre></div></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">最后更新时间:</span> <span class="time">1/29/2021, 5:19:40 PM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev"><a href="/front-end/js/1.全选、函数参数、下拉框、五星评价.html" class="prev"><i aria-label="icon: left" class="anticon anticon-left"><svg viewBox="64 64 896 896" focusable="false" data-icon="left" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M724 218.3V141c0-6.7-7.7-10.4-12.9-6.3L260.3 486.8a31.86 31.86 0 0 0 0 50.3l450.8 352.1c5.3 4.1 12.9.4 12.9-6.3v-77.3c0-4.9-2.3-9.6-6.1-12.6l-360-281 360-281.1c3.8-3 6.1-7.7 6.1-12.6z"></path></svg></i>
        1.全选、函数参数、下拉框、五星评价
      </a></span> <span class="next"><a href="/front-end/js/11.web基本概念、ajax.html">
        11.web基本概念、ajax
        <i aria-label="icon: right" class="anticon anticon-right"><svg viewBox="64 64 896 896" focusable="false" data-icon="right" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M765.7 486.8L314.9 134.7A7.97 7.97 0 0 0 302 141v77.3c0 4.9 2.3 9.6 6.1 12.6l360 281.1-360 281.1c-3.9 3-6.1 7.7-6.1 12.6V883c0 6.7 7.7 10.4 12.9 6.3l450.8-352.1a31.96 31.96 0 0 0 0-50.4z"></path></svg></i></a></span></p></div> </main> <!----></div><div class="global-ui"><!----><div id="goTop" class="hide-cat" data-v-bf92849a></div></div></div>
    <script src="/assets/js/app.419c42a0.js" defer></script><script src="/assets/js/3.2e7f4496.js" defer></script><script src="/assets/js/31.f0b09253.js" defer></script>
  </body>
</html>